package wicket.in.action.chapter09.jcaptcha;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.octo.captcha.service.image.DefaultManageableImageCaptchaService;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImageCaptchaServlet extends HttpServlet {

  private static final ImageCaptchaService captchaService = new DefaultManageableImageCaptchaService();

  @Override
  protected void doGet(HttpServletRequest request,
      HttpServletResponse response) throws ServletException,
      IOException {

    byte[] captchaChallengeAsJpeg = null;
    ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();

    String captchaId = request.getSession(true).getId();

    BufferedImage challenge = captchaService.getImageChallengeForID(
        captchaId, request.getLocale());
    JPEGImageEncoder jpegEncoder = JPEGCodec
        .createJPEGEncoder(jpegOutputStream);
    jpegEncoder.encode(challenge);
    captchaChallengeAsJpeg = jpegOutputStream.toByteArray();

    response.setHeader("Cache-Control", "no-store");
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("Expires", 0);
    response.setContentType("image/jpeg");
    ServletOutputStream os = response.getOutputStream();
    os.write(captchaChallengeAsJpeg);
    os.flush();
    os.close();
  }
}